<script type="text/javascript">
	var groupid = 2;
	function openmenu(id) {
		var target = $('#' + id);
		var state = target.attr("class");
		if ($.inArray("glyphicon-plus",state.split(' ')) != -1) {
			target.removeClass("glyphicon-plus");
			target.addClass("glyphicon-minus");
			$('tr[tag="'+id+'"]').show();
		}
		if ($.inArray("glyphicon-minus",state.split(' ')) != -1) {
			target.removeClass("glyphicon-minus");
			target.addClass("glyphicon-plus");
			$('tr[tag="'+id+'"]').hide();
		}
	}

	function AddMenu(level,pid) {
		$('#menuform').attr('action','{{urlfor "Admin.NodeController.AddNode"}}');
		clearForm($('#menuform'));
		buildForm($('#menuform'),{
			Level:level,
			Pid:pid,
			Groupid:groupid
		});
		$('#MenuEdit').modal('show');
	}

	function EditMenu(id) {
		$('#menuform').attr('action','{{urlfor "Admin.NodeController.UpdateNode"}}');
		clearForm($('#menuform'));
		$.getJSON('{{urlfor "Admin.NodeController.GetNode"}}',{id:id},function (data) {
			data["Groupid"] = groupid;
			buildForm($('#menuform'),data);
		});
		$('#MenuEdit').modal('show');
	}

	function DelMenu(id) {
		if ($('tr[tag="'+id+'"]').length > 0) {
			Messenger().post({message: "请先删除子菜单！",type: 'info'});
			return;
		}
		$('#menuform').attr('action','{{urlfor "Admin.NodeController.DelNode"}}');
		clearForm($('#menuform'));
		buildForm($('#menuform'),{
			Id:id,
			Groupid:groupid
		});
		SaveForm();
	}

	function UpdateState(id,state) {
		$('#menuform').attr('action','{{urlfor "Admin.NodeController.UpdateNode"}}');
		clearForm($('#menuform'));
		buildForm($('#menuform'),{
			Id:id,
			Groupid:groupid,
			Status:state
		});
		SaveForm();
	}
	function SaveForm() {
		$('#menuform').submit();
	}


	function clearForm(objE){  
		$(objE).find(':input').each(
			function(){
				switch(this.type){
					case 'hidden':
					case 'passsword':
					case 'select-multiple':
					case 'select-one':
					case 'text':
					case 'textarea':
						$(this).val('');
						break;
					case 'checkbox':
					case 'radio':
						this.checked = false;
				}
			}
		);
	}
	function buildForm(objE,data){ 
		$(objE).find(':input').each(
			function(){
				switch(this.type){
					case 'hidden':
					case 'passsword':
					case 'select-multiple':
					case 'select-one':
					case 'text':
					case 'textarea':
						$(this).val(data[this.name]);
						break;
					case 'radio':
						this.checked = (this.value == data[this.name]);
						break;
					case 'checkbox':
                		this.checked = ((',' + data[this.name] + ',').indexOf(',' + $(this).val() + ',') >= 0);
						break;
				}
			}
		);
	}
</script>